import { socsearrchList, socsearrchDicList, socsearrchDetail } from '@/services/socialmanage';

export default {
  state: {
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    payData: [], // 缴费方式字典值
    detail: {
      empInfo: {},
      list: [],
    },
    formFieldsValue: {},
    expandForm: false,
    monthvalue: [],
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put, select }) {
      const pathname = yield select(state => state.routing.location.pathname);
      let params = payload;
      if (pathname === '/socaccmanagesend/socsearrchsend/index') {
        params = { ...params, paramType: 2 };
      } else if (pathname === '/socaccmanagereceive/socsearrchreceive/index') {
        params = { ...params, paramType: 1 };
      }
      const response = yield call(socsearrchList, params);
      const {
        pageArg: { currentPage, pageSize },
      } = payload; // 页面传值
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getList',
          payload: {
            data: [],
            currentPage: 1,
            pageSize: 10,
          },
        });
      }
    },
    // 获取字典
    *fetchDicList({ payload }, { call, put }) {
      const response = yield call(socsearrchDicList, payload);
      if (response.success) {
        yield put({
          type: 'dicList',
          payload: {
            data: response.data,
          },
        });
      }
    },
    // 获取详情
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(socsearrchDetail, payload);
      if (response.success) {
        yield put({
          type: 'getDetail',
          payload: response.data,
        });
      }
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      const pageArr = [];
      if (list.length) {
        list.forEach(item => {
          if (item.empAssList.length) {
            // 基数 empBase+ unitBase
            // 个人比例  empProp
            // 个人缴费  empAmo
            // 公司比例  unitProp
            // 公司缴费  unitAmo
            const obj = {};
            item.empAssList.forEach(c => {
              obj[`empBase_${c.ass}`] = c.empBase || +c.empBase === 0 ? c.empBase : '--';
              obj[`unitBase_${c.ass}`] = c.unitBase || +c.unitBase === 0 ? c.unitBase : '--';
              obj[`empProp_${c.ass}`] = c.empProp || +c.empProp === 0 ? c.empProp : '--';
              obj[`empAmo_${c.ass}`] = c.empAmo || +c.empAmo === 0 ? c.empAmo : '--';
              obj[`unitProp_${c.ass}`] = c.unitProp || +c.unitProp === 0 ? c.unitProp : '--';
              obj[`unitAmo_${c.ass}`] = c.unitAmo || +c.unitAmo === 0 ? c.unitAmo : '--';
              obj[`indvAccoNo_${c.ass}`] =
                c.indvAccoNo || +c.indvAccoNo === 0 ? c.indvAccoNo : '--';
            });
            pageArr.push({ ...item, ...obj });
          }
        });
      }
      return {
        ...state,
        data: {
          list: pageArr,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 获取字典
    dicList(
      state,
      {
        payload: { data },
      }
    ) {
      return {
        ...state,
        payData: data.PAY_FEES_WAY,
        tabList: data.BIZ_INS_TYPE,
      };
    },
    // 获取详情
    getDetail(state, { payload }) {
      return {
        ...state,
        detail: payload,
      };
    },

    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
